#!/bin/bash

set -e

# Extracts a docker tag from a passed in git branch name
# branch2tag <branch>
# ie:
# branch2tag develop -> develop
# branch2tag master -> master
# branch2tag release/explorer -> candidate-explorer
# branch2tag release/explorer-0.1.0 -> candidate-0.1.0
# branch2tag release/explorer-0.1.0-rc1 -> candidate-0.1.0-rc1
# branch2tag release/chainlink -> candidate-chainlink
# branch2tag release/0.42.1 -> candidate-0.42.1
# branch2tag release/0.42.1-rc1 -> candidate-0.42.1-rc1
# branch2tag hello -> ""

branch=`echo $1 | awk '{print tolower($0)}'`
if [ "${branch}" = "develop" ]; then
  echo develop
elif [ "${branch}" = "master" ]; then
  echo latest
elif [[ "${branch}" =~ ^release(s)?\/([^-]+-)?(.+)$ ]]; then
  if [ "${BASH_REMATCH[2]}" = "explorer-" ]; then
    echo "candidate-${BASH_REMATCH[3]}"
  else
    echo "candidate-${BASH_REMATCH[2]}${BASH_REMATCH[3]}"
  fi
else
  echo ""
fi
